#include "common.h"


void board_init(void) {

	port_init();

	timer_init();
    
	rEXTINT = 0x22222222;                  // level mode
    rI_ISPC = 0xffffffff;			/* clear all interrupt pend	*/
	rEXTINTPND = 0xf;				// clear EXTINTPND reg


	uart_init(0,115200);

	
	
}

void port_init(void) {

#if 1
	rPCONA = 0x1ff;
	rPDATB = 0x7ff;    
	rPCONB = 0x1cf; 
	rPDATC = 0xff00;          
	rPCONC = 0x0ff0ffff; 
	rPUPC  = 0x30ff;   
	rPDATD = 0xff;             
	rPCOND = 0xaaaa;      
	rPUPD  = 0x0;       
	rPDATE = 0x1ff;               
	rPCONE = 0x25529;        
	rPUPE  = 0x6;          

	rPDATF = 0x0;                 
	rPCONF = 0x252a;         
	rPUPF  = 0x0;       

	rPDATG = 0xff;
	rPCONG = 0xffff;
	rPUPG  = 0x0;
	rSPUCR = 0x7;

	rEXTINT=0x0;
#else
	rPCONA = 0x1ff;	

	rPDATB = 0x04f;
	rPCONB = 0x7cf;    
    
	rPDATC = 0x0000;
	rPCONC = 0x5f555555;	
	rPUPC  = 0x3000;

	rPDATD= 0x55;
	rPCOND= 0xaaaa;	
	rPUPD = 0x00;

	rPDATE	= 0x157;				
	rPCONE	= 0x556b;				
	rPUPE	= 0xff;
	
	rPDATF = 0x0;
	rPCONF = 0x24900a;	 
	rPUPF  = 0x1d3;

	rPDATG = 0xff;
	rPCONG = 0x000f;	//enable extint0 extint1
	rPUPG  = 0x00;		//enabled pull up register

	rSPUCR= 0x7;  //D15-D0 pull-up disable
	//rSPUCR= 0x6;  //D15-D0 pull-up enable
	
	//rSYSCFG = 0x0e;				//使用8K字节的指令缓存,write buffer disable		 
	//rNCACHBE0 = 0xc0000000;		//定义非Cache区,从0x0000000到0xbffffff	烧写FLASH时不能对FLASH cache
	rEXTINT = 0x12;				//所有的外部硬件中断为低电平触发, extint0 为下降沿，extint1为上升沿	
	rINTCON = 5;				// non vector mode, irq enable
	rINTMOD = 0;				//all are irq mode
//		rINTMSK = BIT_EINT1|BIT_GLOBAL;
#endif
	
}



void timer_init(void)
{

    rINTMOD=0x0;
	rINTCON=0x1;

    rINTMSK=~( BIT_TIMER0 | BIT_EINT4567 | BIT_GLOBAL);    
	
	#if 1
	rTCFG0= 0x00000064;  //dead zone=0, pre0= 100
	rTCFG1= 0x00000001;  //all interrupt, mux0= 1/4
	rTCNTB0= _CLOCK;     //set T0 count
    rTCON= 0x00000002;   //update T0
    rINTPND = 0x00002000; /* T0 interrupt clear */
	
	rTCON= 0x00000009;   //T0,auto reload and start
	#else
	rTCFG0 = 255;
    rTCFG1 = 0x1;
    rTCNTB0 = 65535;
    rTCMPB0 = 12800;
    /* update TCNO0 */
	rTCON = 0x6;
	/* enable timer */
    rTCON  = 0x19;
    #endif
	
}//uHALr_InitTimers

